{% extends 'wxchat/wxbase.html' %}{% load static %}
{% block extra %}
    {% include 'shopping/include/jssdk.html' %}
    <script type="text/javascript">
    wx.ready(function () {
        $('#pay_now').on('click',function(){
            var params ={
              out_trade_no: $("#out_trade_no").text(),
              csrfmiddlewaretoken:'{{ csrf_token }}',
            }

           $.ajax({
              type: 'POST',
              url: '{% url "dog-pay-order" %}',
              dataType: 'json',
              data: params,
              timeout: 5000,
              success: function(data){
                  if(data.package !=undefined){
                      wxpay(data)
                  }
              },
              error: function(error){
                  weui.alert('商品支付失败!');
                  window.location.href = "{% url 'shopping-list' %}";
              }
            });
        });

        function wxpay(data){
            wx.chooseWXPay({
                  timestamp:data.timestamp,
                  nonceStr: data.nonceStr,
                  package:  data.package ,
                  signType: data.signType,
                  paySign:  data.paySign,
                  success: function(res){
                    if (res.errMsg == "chooseWXPay:ok") {
                        out_trade_no = $("#out_trade_no").text();
                        window.location.replace('{% url "order-list" %}?out_trade_no=' + out_trade_no + '&_result=ok');
                    } else {
                       weui.alert('定制食品支付失败!');
                    }
                  },
                   cancel: function(res) {
                       out_trade_no = $("#out_trade_no").text();
                       updateDogOrder(out_trade_no);
                   },
                   fail:function(res){
                       out_trade_no = $("#out_trade_no").text();
                       updateDogOrder(out_trade_no);
                   }
            });
        }

        function updateDogOrder(out_trade_no, url){
            var params ={
              out_trade_no: out_trade_no,
              csrfmiddlewaretoken:'{{ csrf_token }}',
              action:"update",
            }

           $.ajax({
              type: 'POST',
              url: '{% url "order-list" %}',
              dataType: 'json',
              data: params,
              timeout: 5000,
              success: function(data){
                if(data.success == "true"){
                     $("#out_trade_no").text(data.out_trade_no)
                }
              },
              error: function(error){
                  weui.alert('商品支付失败!');
                  window.location.href = "{% url 'shopping-list' %}";
              }
            });
        }


});

</script>
{% endblock extra %}

{% block container %}
<div class="weui-tab" id="tab">
    <div class="weui-tab__panel">
        <div class="weui-panel weui-panel_access">
       <div class="weui-panel__hd">
            <div class="weui-flex">
                <div><a href="javascript:window.history.back(-1)"><i class="icon iconfont icon-jiantou3 gray " ></i></a></div>
                <div class="weui-flex__item" style="text-align: center;"><span class="detail_title">收银台</span></div>
                <div ><a href="{% url 'shopping-list' %}?next={{ request.get_full_path }}"><i class="icon iconfont icon-shangcheng10 pink r" ></i></a></div>
            </div>
       </div>
        <div class="weui-panel__bd">
            <div class="weui-form-preview">
                <div class="weui-form-preview__hd" style="text-align: right;">
                    <div class="weui-form-preview__item">
                        <label class="weui-form-preview__label">付款金额</label>
                        <em class="weui-form-preview__value font16" id="total_cost_top" > ¥ {{ order.total_fee|floatformat:"2" }}</em>
                        <span id="out_trade_no" style="display: none" >{{ order.out_trade_no }}</span>
                    </div>
                </div>
                <div class="weui-form-preview__bd" style="text-align: right;">
                    <div class="weui-form-preview__item">
                        <label class="weui-form-preview__label">定制价格</label>
                        <em class="weui-form-preview__value " style="color: #c0c0c0;" >¥ {{ order.price|floatformat:"2" }}</em>
                    </div>
                </div>
               <div class="weui-form-preview__bd" style="text-align: right;">
                    <div class="weui-form-preview__item">
                        <label class="weui-form-preview__label">定制数量</label>
                        <em class="weui-form-preview__value " style="color: #c0c0c0;" >{{ order.goods_nums}}</em>
                    </div>
                </div>
                <div class="weui-cells" style="margin-top: 0;">
                    <div class="weui-cells__title">宠物食品定制选项</div>
                    {% for item in order.dogorderitem_set.all %}
                      <div class="weui-cell">
                        <div class="weui-cell__hd"></div>
                        <div class="weui-cell__bd">
                          <p>{{ item.dog_status.short_name }}</p>
                        </div>
                        <div class="weui-cell__ft">  {{ item.dog_status_type }}</div>
                      </div>
                    {% endfor %}
                </div>
            </div>
        </div>
    </div>
    </div>
    <div class="weui-tabbar">
         <a href="#" id="pay_now" class="weui-tabbar__item tabbar_item bg-primary" >
            <p class="weui-tabbar__label " style="font-size:18px;"> 立即支付 <span id="total_cost_bottom"> ¥ {{ order.total_fee }}</span></p>
        </a>

    </div>
</div>

{% endblock container %}

{% block bottomjs %}
    {{ block.super }}
<script>

</script>
{% endblock bottomjs %}